#include "worker.h"
/*
//管理员信息
typedef struct Admin
{
	char id[11];
	char password[7];
	char phone[12];
}Admin;

Admin admin;
*/
bool admin_up_password(void)
{
	char password_[7];
	int cnt_try_admin=0;     
	while(1)
	{
		printf("请输入密码：");
		get_passwd(password_,sizeof(password_));
		if(0 == strcmp(password_ ,admin_password))
		{
			return true;
		}
		if(3 ==cnt_try_admin++)
		{                           
			return false;
        }
		printf("你输入的密码错误，您还有%d次机会，请重新输入\n",4-cnt_try_admin);
	}
}

//工作人员注册
void worker_enroll(void)
{
	Worker worker;

	printf("请输入姓名:");
	get_str(worker.name,sizeof(worker.name));

	printf("请设置密码:");
	get_passwd(worker.password,sizeof(worker.password));
	char repassword[7];
	printf("请再次输入密码:");
	get_passwd(repassword,sizeof(repassword));

	while(strcmp(worker.password,repassword))
	{
        show_msg("两次输入密码不符，注册失败!请重新输入",0.7);
		printf("请设置密码：");
		get_passwd(worker.password,sizeof(worker.password));

		printf("请再次输入密码:");
		get_passwd(repassword,sizeof(repassword));
	}
	printf("请输入电话号码:");
	get_str(worker.phone,sizeof(worker.phone));
    int len=strlen(worker.phone);
	while(len!=11)
	{
		show_msg("电话格式不正确,请重新输入!\n",0.7);
		printf("请输入电话号码:");
	    get_str(worker.phone,sizeof(worker.phone));
        len=strlen(worker.phone);
	}
		
	worker.sex = get_sex();

	int id = make_worker_id();
    if(0 <= id) 
		if(write_worker_id(id,&worker))
    		show_msg("工作人员注册成功!",0.7);
		else
			show_msg("注册失败请!",0.7);
	else
		show_msg("工作人员注册失败请联系管理员!",0.7);
	anykey_continue();
}

//查询管理工作人员
void find_worker(void)
{
	//查询
	int id;
	Worker* worker;

	printf("请输入工号:");
	scanf("%d",&id);
	if(!read_worker_id(id,worker))
	{
		show_msg("未查询到该工作人员",0.7);
		return;
	}
	
	char password[7];
	printf("请输入密码：");
	get_passwd(password,sizeof(password));
	while(0 != strcmp(worker->password,password))
	{
		printf("密码输入错误，请重新输入：");
		get_passwd(password,sizeof(password));
	}
	for(;;)
	{
		show_worker_information(id,worker);
		switch(get_cmd('1','5'))
		{
			//修改姓名
			case '1':{
				printf("请输入新的姓名:");
				scanf("%s",worker->name);
			}break;
			case '2':{
				char password[7];
				printf("请输入旧密码:");
				get_passwd(password,sizeof(password));
				if(strcmp(password,worker->password))
				{
					show_msg("旧密码错误无法修改!",0.7);
					return;
				}
				printf("请输入新密码:");
				get_passwd(password,sizeof(password));

				char repassword[7];
				printf("请确认新密码:");
				get_passwd(repassword,sizeof(repassword));

				if(0==strcmp(password,repassword))
				{
					strcpy(worker->password,password);
					show_msg("修改密码成功!",0.7);
				}
				else
				{
					show_msg("两次输入密码不符，修改失败!",0.7);
				}
			}break;
			case '3':{
				printf("请输入新的号码:");
				scanf("%s",worker->phone);
			}break;
			case'4':{
				if(del_worker_id(id))
					show_msg("注销成功!",0.7);
				else 
					show_msg("注销失败!",0.7);

			}return;
			case '5':write_worker_id(id,worker);return;
		}
	}
}

//显示修改工作人员信息界面
void show_worker_information(int id,Worker* worker)
{
	system("clear");
	puts("湖畔图书馆管理系统");
	puts("*****欢迎使用*****");
	printf("\n姓名: %s\n工号: %d\n密码: %s\n电话号码: %s\n性别: %c\n",
		worker->name,
		id,
		worker->password,
		worker->phone,
		worker->sex);
	printf("-------------------\n");
	puts("1.修改姓名");
	puts("2.修改密码");
	puts("3.修改号码");
	puts("4.注销");
	puts("5.退出");
}


//遍历工作人员
void worker_show_all(void)
{
	FILE* rfp = fopen(WORKER_ALL_PATH,"r");
	if(NULL == rfp)
	{
        return ;
	}
	All all;
	fscanf(rfp,"%d %d",&all.num,&all.cnt);
	
	int id[all.num];
	if(0 == all.num)
		printf("无工作人员在册！");
	for(int i=0;i<all.num;i++)
	{
		fscanf(rfp,"%d",&id[i]);
		Worker worker;
		read_worker_id(id[i],&worker);
		printf("%d.姓名：%s  工号：%d  号码：%s  性别：%c",
					i+1,
					worker.name,
					id[i],
					worker.phone,
					worker.sex);
	}
	fclose(rfp);
	anykey_continue();
}

//工作人员信息菜单
void show_worker_ml(void)
{
	system("clear");
	puts("工作人员信息管理");
	puts("1.工作人员注册");
	puts("2.工作人员查询管理");
	puts("3.查看所有人员");
	puts("4.返回上一界面");
}

//管理工作人员信息界面
void worker_ml(void)
{
	if(!admin_up_password())
	{
		printf("登录失败！");
		return;
	}

	for(;;)
	{
		show_worker_ml();
		switch(get_cmd('1','4'))
		{
			case '1':worker_enroll();break;
			case '2':find_worker();break;
			case '3':worker_show_all();break;
			case '4':return;
		}
	}
}
